package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CarWorksAdapter;
import flc.ast.databinding.ActivityMyWorksBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class MyWorksActivity extends BaseAc<ActivityMyWorksBinding> {

    private CarWorksAdapter worksAdapter;
    private List<String> listShow = new ArrayList<>();
    private Dialog myDeleteDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_my_works;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this,mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this,mDataBinding.event5Container);
        mDataBinding.ivMyWorksBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivMyWorksClear.setOnClickListener(this);
        mDataBinding.rvMyWorksList.setLayoutManager(new GridLayoutManager(mContext, 2));
        worksAdapter = new CarWorksAdapter();
        mDataBinding.rvMyWorksList.setAdapter(worksAdapter);
        worksAdapter.setOnItemClickListener(this);
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMyWorksClear:
                if (listShow.size()==0){
                    ToastUtils.showShort(getString(R.string.no_record));
                    return;
                }
                myDeleteDialog.show();
                break;
            //清空弹框
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确定
                if (listShow.size() == 0) {
                    return;
                }
                myDeleteDialog.dismiss();
                deleteWorks();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        WorkDetailActivity.imgPath=worksAdapter.getItem(position);
        startActivity(WorkDetailActivity.class);
    }

    @Override
    protected void initData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myDraw);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            worksAdapter.setList(listShow);
            mDataBinding.rvMyWorksList.setVisibility(View.VISIBLE);
            mDataBinding.ivWorksNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvMyWorksList.setVisibility(View.GONE);
            mDataBinding.ivWorksNoData.setVisibility(View.VISIBLE);
        }
    }

    // 清空
    private void deleteWorks() {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (String path : listShow) {
                    FileUtils.delete(path);
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                initData();
                dismissDialog();
                ToastUtils.showShort(R.string.clear_suc);
            }
        });
    }

    //清空弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteRight);
        TextView text = inflate.findViewById(R.id.tvDialogDeleteText);
        text.setText(getString(R.string.is_clear));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }
}